package com.lwj.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwj.common.util.PageResult;
import com.lwj.entity.Flower;
import com.lwj.entity.FlowerType;
import com.lwj.mapper.FlowerMapper;
import com.lwj.service.FlowerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwj.service.FlowerTypeService;
import com.lwj.vo.FlowerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
public class FlowerServiceImpl extends ServiceImpl<FlowerMapper, Flower> implements FlowerService {

	@Autowired
	private FlowerTypeService flowerTypeService;
	@Override
	public PageResult<Flower> findByPage(Integer page, Integer pageSize) {
		Page<Flower> page1 = this.page(new Page<>(page, pageSize));
		return new PageResult<>(page1.getRecords(),page1.getTotal());
	}

	@Override
	public PageResult<Flower> search(Integer page, Integer pageSize, FlowerVO vo) {
		QueryWrapper<Flower> wrapper = new QueryWrapper<>();
		wrapper.like(StrUtil.isNotBlank(vo.getFlowerName()), "flowerName",  vo.getFlowerName() )
			   .like(StrUtil.isNotBlank(vo.getFlowerOrigin()), "flowerOrigin", vo.getFlowerOrigin())
				.eq(vo.getAttributeId() != null && vo.getAttributeId() != 0, "attributeId", vo.getAttributeId())
			   .eq(vo.getFlowerPrice() != null && vo.getFlowerPrice() != 0, "flowerPrice", vo.getFlowerPrice());

		Page<Flower> page1 = this.page(new Page<>(page, pageSize), wrapper);
		page1.getRecords().forEach(st -> {
			FlowerType flowerType = flowerTypeService.getById(st.getAttributeId());
			st.setAttributeName(flowerType.getAttributeName());
		});
		return new PageResult<>(page1.getRecords(),page1.getTotal());
	}
	@Override
	public boolean add(Flower flower) {

		return this.save(flower);
	}
}
